package com.tusuo.stds.biz;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.tusuo.stds.Constants;
import com.tusuo.stds.rest.AppInfo;
import com.tusuo.stds.rest.AreaList;

import myandroid.library.util.CookieHelper;

/**
 * @author J.Huang
 * @version 1.0
 */

public class CurrentUser {

    private final SharedPreferences mSharedPref;

    private AppInfo mAppInfo = null;
    private AreaList mAreaList = null;

    private boolean mSimilarityEnabled = false;

    public CurrentUser(SharedPreferences sharedPref) {
        mSharedPref = sharedPref;
    }

    public void setAppInfo(AppInfo appInfo) {
        mAppInfo = appInfo;
    }

    public boolean hasLoggedIn() {
        if (mAppInfo != null) {
            if ("login".equals(mAppInfo.UserStatus.toLowerCase())) {
//                if (!TextUtils.isEmpty(mAppInfo.CookieName)) {
//                    SharedPreferences.Editor editor = mSharedPref.edit();
//                    editor.putString(Constants.KEY_COOKIE, mAppInfo.CookieName);
//                    editor.apply();
//                }
                return true;
            }
        }

        return false;
    }

    public int getLatestAppVersion() {
        return mAppInfo != null ? mAppInfo.AppVersion : 0;
    }

    public String getAppDownloadUrl() {
        return mAppInfo != null ? mAppInfo.AppUrl : "";
    }

    public boolean hasAppForcedUpdate() {
        return mAppInfo != null && mAppInfo.Update;
    }

    public boolean hasMultiLoggedIn() {
        return (mAppInfo != null) && "multilogin".equals(mAppInfo.UserStatus.toLowerCase());
    }

    public void logout() {
        setAppInfo(null);

        // 删除保存的用户信息
        SharedPreferences.Editor editor = mSharedPref.edit();
        editor.putString(Constants.KEY_COOKIE, "");
        editor.apply();

//        CookieHelper.RemoveCookie(null);
    }

    public void setAreaList(AreaList areaList) {
        mAreaList = areaList;
    }

    public AreaList getAreaList() {
        return mAreaList;
    }

    public void setOpenSimilarityState(boolean similarityEnabled) {
        mSimilarityEnabled = similarityEnabled;
    }

    public boolean getOpenSimilarityState() {
        return mSimilarityEnabled;
    }
}
